//
//  UserAboutUsController.m
//  CheekiOS
//
//  Created by mac on 2017/7/2.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserAboutUsController.h"
//用户协议
#import "UserAgreementWebController.h"
@interface UserAboutUsController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSString *VersionStr;

@property (nonatomic, strong) NSString *structureStr;

@property (nonatomic, strong) NSNumber *UserTypehidden;
@end

@implementation UserAboutUsController

-(void)viewWillAppear:(BOOL)animated{
    
    [self setNavgationBarHiddenNo];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self selfTitleFontColor];
    self.title = @"关于我们";
    _structureStr = @"";
    [HcardManagerTool requestWithURLString:@"https://itunes.apple.com/lookup?id=1252934263" parameters:nil type:HttpRequestTypePost success:^(id result) {
        NSArray *array = result[@"results"];
        NSDictionary *dict = [array lastObject];
        NSLog(@"当前版本为：%@", dict[@"version"]);
        _VersionStr = dict[@"version"];
    } failure:^(NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:@"请求失败！" ];
        
    }];
    
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 200)];
    bgView.backgroundColor = self.view.backgroundColor;
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.centerX-30, 80, 60, 60)];
    imageView.image = [UIImage imageNamed:@"about_us_logo_icon"];
    [bgView addSubview:imageView];
    
    
    //获取设备基本信息
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    //app名称
    NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];

    UILabel *VersionLab = [[UILabel alloc] initWithFrame:CGRectMake(0, imageView.bottom+25, KScreenWidth, 20)];
    VersionLab.font = FontP(12);
    VersionLab.textColor = ColorI(0x2e353f);
    VersionLab.text  = [NSString stringWithFormat:@"%@v%@",app_Name,app_Version];
    VersionLab.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:VersionLab];
    
    //获取商店版本号
    
   
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = RGB(246, 246, 246);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _tableView.userInteractionEnabled = NO;
        _tableView.tableHeaderView = bgView;
        [self.view addSubview:_tableView];
    }
    
    if ([app_Version isEqualToString: _VersionStr] || _VersionStr == nil) {
        _structureStr = @"无新版本";
        [_tableView reloadData];

    }
    
    
    if (_VersionStr > app_Version) {
        _structureStr = @"有新版本请更新";
        [_tableView reloadData];
    }
    
    [self requestgetCommonOpen];
}

- (void)requestgetCommonOpen{
    
    [HcardHttpmanager getCommonOpen:[HQHttpManager getUserId] success:^(id result) {
        if([result[@"code"] isEqualToString:@"ok"]){
            
            _UserTypehidden = result[@"res"][@"open"];
            
            [_tableView reloadData];
        }
        
    } failure:^(NSError *error) {
        
    }];
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([_UserTypehidden isEqualToNumber:@1]) {
        return 2;
    }else{
        return 1;

    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        static NSString *AboutUsCellOneIdent = @"AboutUsCellOneIdent";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:AboutUsCellOneIdent];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AboutUsCellOneIdent];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        UILabel *linLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 1)];
        linLab.backgroundColor = TheLinColor;
        [cell.contentView addSubview:linLab];
        
        
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(15, 1, 100, 60)];
        lab.textColor = [UIColor blackColor];
        lab.text = @"用户协议";
        [cell.contentView addSubview:lab];
        
        cell.backgroundColor = [UIColor whiteColor];


        return cell;
    }else{
        static NSString *AboutUsCellTwoIdent = @"AboutUsCellTwoIdent";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:AboutUsCellTwoIdent];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AboutUsCellTwoIdent];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.backgroundColor = [UIColor whiteColor];
        
        UILabel *linLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 1)];
        linLab.backgroundColor = TheLinColor;
        
        
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(15, 1, 100, 60)];
        lab.textColor = [UIColor blackColor];
        lab.text = @"版本更新";
        
        UILabel *versionLab = [[UILabel alloc] initWithFrame:CGRectMake(KScreenWidth-115, 1, 100, 60)];
        versionLab.textColor = [UIColor blackColor];
        versionLab.text = _structureStr;
        versionLab.font = FontP(15);
        versionLab.textAlignment = NSTextAlignmentRight;
        
        UILabel *lintwo = [[UILabel alloc]initWithFrame:CGRectMake(0, versionLab.bottom, KScreenWidth, 1)];
        lintwo.backgroundColor = TheLinColor;

        
        
        [cell.contentView addSubview:linLab];

        [cell.contentView addSubview:lab];
        
        [cell.contentView addSubview:versionLab];
        
        [cell.contentView addSubview:lintwo];
        

        return cell;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 61;
    }else{
        return 62;
    }

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        UserAgreementWebController *VC = [[UserAgreementWebController alloc] init];
        VC.UrlStr = HttpUrlStringS(@"userxieyi.html");
        VC.setTitle = @"用户协议";
        [self.navigationController pushViewController:VC animated:YES];
    }else {
        if ([_structureStr isEqualToString:@"有新版本请更新"]) {
            //@""
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/tie-lian-er/id1252934263?mt=8"]];
        }else{
  
            NSLog(@"无新版本");
        }
    }
}

@end
